package com.qf.controller;

import com.qf.dto.StudentDto;
import com.qf.pojo.TbCource;
import com.qf.pojo.TbStudent;
import com.qf.pojo.TbVariable;
import com.qf.result.R;
import com.qf.service.StudentPlusService;
import com.qf.service.StudentService;
import com.qf.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * created by 赵文韬 on 2022/5/27 10:49
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentPlusService studentPlusService;
    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }
        //学生注册
    @PostMapping("/regist")
    public R register(@RequestBody StudentDto studentDto){
        String dtoUsername = studentDto.getUsername();
        TbStudent tbStudent = studentService.selectByName(dtoUsername);
        if (tbStudent==null){
            studentService.insertStudent(studentDto);
        }else {
            return R.error().setMsg("用户名已存在");
        }
        return R.ok().setMsg("注册成功");

    }
    //学生登录
    @PostMapping("/login")
    public R studentLogin(@RequestBody StudentDto studentDto){
        String username = studentDto.getUsername();
        String password = studentDto.getRepassword();
        TbStudent tbStudent = studentService.selectByNameAndPassword(username, password);
        if (tbStudent!=null){
            return R.ok().setMsg("登陆成功").setResult(tbStudent);
        }else {
            return R.error().setMsg("用户名或密码错误");
        }
    }
    //删除选课
    @PostMapping("/deleteVariable")
    public R deleteVariable(@RequestBody int stuId,int cid){
        TbVariable tbVariable = new TbVariable();
        tbVariable.setSid(stuId);
        tbVariable.setCid(cid);
        int result = studentService.delCource(tbVariable);
        if (result==0){
            return R.error().setMsg("删除失败");
        }else {
            return R.ok().setMsg("删除成功");
        }
    }
    //添加选课
    @PostMapping("/addVariable")
    public R addVariable(@RequestBody int stuId,int cid){
        TbVariable tbVariable = new TbVariable();
        tbVariable.setSid(stuId);
        tbVariable.setCid(cid);
        int result = studentService.addCource(tbVariable);
        if (result==0){
            return R.error().setMsg("添加失败");
        }else {
            return R.ok().setMsg("添加成功");
        }
    }
    //根据学生id查找所选课程
    @PostMapping("/selctVariable")
    public R selectVariable(@RequestBody int stuId){

        List<TbCource> tbCources = studentService.selectById(stuId);
        if (tbCources.size()==0){
            return R.error().setMsg("添加失败");
        }else {
            return R.ok().setMsg("添加成功").setResult(tbCources);
        }
    }
    @PostMapping("/selectone")
    public R selectOne(@RequestBody StudentDto studentDto){
        TbStudent student = BeanUtils.copy(studentDto, TbStudent.class);
        TbStudent tbStudent = studentPlusService.selectS(student);
        return R.ok().setResult(tbStudent);
    }


}
